//
//  ReleaseStep1ViewController.m
//  Gunz
//
//  Created by Joggy on 16/4/13.
//  Copyright © 2016年 NUPT. All rights reserved.
//

#import "ReleaseStep1ViewController.h"
#import "ReleaseStep2ViewController.h"
#import "GZConst.h"
#import "BarButtonView.h"
#import "GZLoginState.h"
#import "QYNotice.h"
#import "GZLoginState.h"

@interface ReleaseStep1ViewController ()

@end

@implementation ReleaseStep1ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self prepareForNav];
    [self prepareForLayout];
    _data = [[NSMutableDictionary alloc] initWithCapacity:0];
    textBottom = 0;
    scrollOrigin = scroll.frame;
    [scroll addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(endEdit)]];
    scroll.delegate = self;
//    _data = @{@"uid": @"", @"ownuser": @"", @"taskmodel": @"", @"trademodel": @"", @"peoplenum": @"", @"faretask": @"", @"tendermodel": @"", @"typetask": @"", @"requiretask": @"", @"aimtask": @"", @"realtask": @"", @"name": @"", @"describe": @"", @"industry": @"", @"subdescribe": @"", @"subtasknum": @"", @"datatype": @"", @"dataformat": @"", @"lasttime": @"", @"starttime": @"", @"overtime": @"", @"icontask": @"", @"address": @"", @"demo": @"", @"template": @"", @"": @"", @"": @"", @"": @""};
    NSString *userID = [GZLoginState getCurrentLoginUserID];
    _data[@"uid"] = userID;
    _data[@"ownuser"] = userID;
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}

- (void)prepareForNav {
    [super prepareForNav];
    self.title = @"任务发布-步骤1";
    //导航左按键
    UIImage *image = [UIImage imageNamed:@"all_back"];
    UIButton *leftBarButton = [UIButton buttonWithType:UIButtonTypeSystem];
    leftBarButton.frame = CGRectMake(0, 0, image.size.width, image.size.height);
    leftBarButton.layer.position = CGPointMake(22, 22);
    [leftBarButton setImage:[image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    [leftBarButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftBarButton];
}

- (void)prepareForLayout {
    [super prepareForLayout];
    scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, [GZConst getScreenWidth], [GZConst getScreenHeight] - 64 - 56)];
    scroll.contentSize = CGSizeMake(scroll.frame.size.width, scroll.frame.size.height + 1);
    [self.view addSubview:scroll];
    //底部按钮
    BarButtonView *barButton = [[BarButtonView alloc] initWithFrame:CGRectMake(0, [GZConst getScreenHeight] - 64 - BBViewHeight, [GZConst getScreenWidth], BBViewHeight) name:@"下一步"];
    [barButton.but addTarget:self action:@selector(nextStep) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:barButton];
    //标题lab
    UILabel *titleConfig = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 104, 17)];
    titleConfig.layer.position = CGPointMake([GZConst getScreenWidth]/2, 34);
    titleConfig.textColor = CUSTOM_GREEN;
    titleConfig.font = [UIFont systemFontOfSize:14];
    titleConfig.text = @"交易模式配置";
    [scroll addSubview:titleConfig];
    //选择模式
    select = [[Step1Select alloc] initWithMode:@"选择模式" one:@"自动" two:@"人工"];
    select.layer.position = CGPointMake([GZConst getScreenWidth]/2, 73);
    [select.pointOne addTarget:self action:@selector(autoSelected) forControlEvents:UIControlEventTouchUpInside];
    [select.pointTwo addTarget:self action:@selector(manSelected) forControlEvents:UIControlEventTouchUpInside];
    [scroll addSubview:select];
    [self adjustScrollContent:select.frame.origin.y + select.frame.size.height];
}

- (void)autoSelected {
    select.pointOne.selected = !select.pointOne.selected;
    if(select.pointOne.selected) {
        select.pointTwo.selected = false;
        if(deal != nil) {
            [deal removeFromSuperview];
        }
        if(textBidInput != nil) {
            [textBidInput removeFromSuperview];
        }
        if(textbudgetInput != nil) {
            [textbudgetInput removeFromSuperview];
        }
        if(bid != nil) {
            [bid removeFromSuperview];
        }
        if(textRewardInput != nil) {
            [textRewardInput removeFromSuperview];
        }
    }
}

- (void)manSelected {
    select.pointTwo.selected = !select.pointTwo.selected;
    if(select.pointTwo.selected) {
        select.pointOne.selected = false;
        deal = [[Step1Select alloc] initWithMode:@"交易模式:" one:@"招标" two:@"悬赏"];
        deal.layer.position = CGPointMake([GZConst getScreenWidth]/2, 110);
        [deal.pointOne addTarget:self action:@selector(bidding) forControlEvents:UIControlEventTouchUpInside];
        [deal.pointTwo addTarget:self action:@selector(reward) forControlEvents:UIControlEventTouchUpInside];
        [scroll addSubview:deal];
        [self adjustScrollContent:deal.frame.origin.y + deal.frame.size.height];
    }
    else {
        if(deal != nil) {
            [deal removeFromSuperview];
        }
        if(textBidInput != nil) {
            [textBidInput removeFromSuperview];
        }
        if(textbudgetInput != nil) {
            [textbudgetInput removeFromSuperview];
        }
        if(bid != nil) {
            [bid removeFromSuperview];
        }
        if(textRewardInput != nil) {
            [textRewardInput removeFromSuperview];
        }
    }
}

- (void)bidding {
    deal.pointOne.selected = !deal.pointOne.selected;
    if(deal.pointOne.selected) {
        deal.pointTwo.selected = false;
        if(textRewardInput != nil) {
            [textRewardInput removeFromSuperview];
        }
        textBidInput = [[Step1Input alloc] initWithTitle:@"竞标人数:"];
        textBidInput.layer.position = CGPointMake([GZConst getScreenWidth]/2, 150);
        textBidInput.textField.tag = 1;
        textBidInput.textField.delegate = self;
        [scroll addSubview:textBidInput];
        textbudgetInput = [[Step1Input alloc] initWithTitle:@"预算金额:"];
        textbudgetInput.layer.position = CGPointMake([GZConst getScreenWidth]/2, 190);
        textbudgetInput.textField.tag = 2;
        textbudgetInput.textField.delegate = self;
        [scroll addSubview:textbudgetInput];
        bid = [[Step1Select alloc] initWithMode:@"招标形式:" one:@"荷式" two:@"英式"];
        bid.layer.position = CGPointMake([GZConst getScreenWidth]/2, 230);
        [bid.pointOne addTarget:self action:@selector(heshi) forControlEvents:UIControlEventTouchUpInside];
        [bid.pointTwo addTarget:self action:@selector(yingshi) forControlEvents:UIControlEventTouchUpInside];
        [scroll addSubview:bid];
        [self adjustScrollContent:bid.frame.origin.y + bid.frame.size.height];
    }
    else {
        if(textBidInput != nil) {
            [textBidInput removeFromSuperview];
        }
        if(textbudgetInput != nil) {
            [textbudgetInput removeFromSuperview];
        }
        if(bid != nil) {
            [bid removeFromSuperview];
        }
    }
}

- (void)reward {
    deal.pointTwo.selected = !deal.pointTwo.selected;
    if(deal.pointTwo.selected) {
        deal.pointOne.selected = false;
        if(textBidInput != nil) {
            [textBidInput removeFromSuperview];
        }
        if(textbudgetInput != nil) {
            [textbudgetInput removeFromSuperview];
        }
        if(bid != nil) {
            [bid removeFromSuperview];
        }
        textRewardInput = [[Step1Input alloc] initWithTitle:@"悬赏金额:"];
        textRewardInput.layer.position = CGPointMake([GZConst getScreenWidth]/2, 150);
        textRewardInput.textField.tag = 3;
        textRewardInput.textField.delegate = self;
        [scroll addSubview:textRewardInput];
        [self adjustScrollContent:textRewardInput.frame.origin.y + textRewardInput.frame.size.height];
    }
    else {
        if(textRewardInput != nil) {
            [textRewardInput removeFromSuperview];
        }
    }
}

- (void)heshi {
    bid.pointOne.selected = !bid.pointOne.selected;
    if(bid.pointOne.selected) {
        bid.pointTwo.selected = false;
    }
}

- (void)yingshi {
    bid.pointTwo.selected = !bid.pointTwo.selected;
    if(bid.pointTwo.selected) {
        bid.pointOne.selected = false;
    }
}

- (void)adjustScrollContent:(CGFloat)bottom {
    if(scroll.frame.size.height < bottom) {
        scroll.contentSize = CGSizeMake(scroll.frame.size.width, bottom);
    }
    else {
        scroll.contentSize = CGSizeMake(scroll.frame.size.width, scroll.frame.size.height + 1);
    }
}

- (NSString*)collectData {
    if(!select.pointOne.selected && !select.pointTwo.selected) {
        return @"请选择任务模式";
    }
    else {
        if(select.pointOne.selected) {
            _data[@"taskmodel"] = @"2";
            _data[@"trademodel"] = @"0";
            return @"noerror";
        }
        else {
            if(!deal.pointOne.selected && !deal.pointTwo.selected) {
                return @"请选择交易模式";
            }
            else {
                if(deal.pointOne.selected) {
                    _data[@"trademodel"] = @"2";
                    if([textBidInput.textField.text  isEqualToString: @""]) {
                        return @"请输入竞标人数";
                    }
                    else {
                        _data[@"peoplenum"] = textBidInput.textField.text;
                    }
                    if([textbudgetInput.textField.text isEqualToString:@""]) {
                        return @"请输入预算金额";
                    }
                    else {
                        _data[@"faretask"] = textbudgetInput.textField.text;
                    }
                    if(!bid.pointOne.selected && !bid.pointTwo.selected) {
                        return @"请选择招标形式";
                    }
                    else {
                        if(bid.pointOne.selected) {
                            _data[@"tendermodel"] = @"1";
                        }
                        else {
                            _data[@"tendermodel"] = @"2";
                        }
                    }
                    return @"noerror";
                }
                else {
                    _data[@"trademodel"] = @"1";
                    if([textRewardInput.textField.text isEqualToString:@""]) {
                        return @"请输入悬赏金额";
                    }
                    else {
                        _data[@"faretask"] = textRewardInput.textField.text;
                    }
                    return @"noerror";
                }
            }
        }
    }
}

- (void)nextStep {
    NSString *errorInfo = [self collectData];
    if([errorInfo isEqualToString:@"noerror"]) {
        ReleaseStep2ViewController *nextStep = [[ReleaseStep2ViewController alloc] init];
        nextStep.data = _data;
        [self.navigationController pushViewController:nextStep animated:true];
    }
    else {
        [QYNotice showError:errorInfo toView:self.view];
        [QYNotice dismissWithDelay:3];
    }
}

- (void)endEdit {
    [self.view endEditing:true];
}

- (void)back {
    [self dismissViewControllerAnimated:true completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)keyboardWillShow:(NSNotification*)notification {
    NSDictionary *dic = notification.userInfo;
    CGFloat keyboardHeight = [[dic objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height;
    CGFloat adjust = textBottom - ([GZConst getScreenHeight] - keyboardHeight - 64);
    CGRect temp = scrollOrigin;
    if(adjust > 0) {
        temp.origin.y -= adjust;
        [UIView animateWithDuration:0.3 animations:^{
            scroll.frame = temp;
        }];
    }
    else {
        [UIView animateWithDuration:0.3 animations:^{
            scroll.frame = temp;
        }];
    }
}

- (void)keyboardWillHide:(NSNotification*)notification {
    [UIView animateWithDuration:0.3 animations:^{
        scroll.frame = scrollOrigin;
    }];
}

- (void)textFieldDidBeginEditing:(UITextField *)textField {
    switch(textField.tag) {
        case 1:
            textBottom = textBidInput.frame.origin.y + textBidInput.frame.size.height;
            break;
        case 2:
            textBottom = textbudgetInput.frame.origin.y + textbudgetInput.frame.size.height;
            break;
        case 3:
            textBottom = textRewardInput.frame.origin.y + textRewardInput.frame.size.height;
            break;
        default:
            textBottom = 0;
            break;
    }
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    [self endEdit];
}

@end
